const express = require('express')
const router = express.Router()
const mysql = require("../mysql/config")
let connection = null
const resSend = require('../utils/resSend')

router.post("/add",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.body.title,req.body.content,JSON.stringify(req.body.imageList),req.body.goodsId || 0,req.user.id,req.body.location || " ",req.body.money || 0]
    // 添加文章
    connection.query(`INSERT INTO article_table (title,content,imageList,goodsId,userId,location,money) VALUES (?,?,?,?,?,?,?)`,params,(err,result)=>{
        if(err){
            console.log(err);
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

// 修改文章
router.post("/update",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.body.title,req.body.content,JSON.stringify(req.body.imageList),req.body.goodsId,req.body.location || " ",req.body.money || 0,req.body.id]
    // 修改文章
    connection.query(`UPDATE article_table SET title=?,content=?,imageList=?,goodsId=?,location=?,money=? WHERE id=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

// 删除
router.post("/delete",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.body.id]
    // 删除文章
    connection.query(`DELETE FROM article_table WHERE id=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})


// 获取商品名称以及ID
router.get("/getGoodsNameAndId",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    connection.query(`SELECT id,title,imageList,location,money FROM article_table WHERE goodsId=0`,(err,result)=>{
        if(err){
            resSend(res,500,'获取商品名称以及ID失败',err)
        }else{
            if (result.length > 0) {
                result.forEach(val => {
                    val.imageList = JSON.parse(val.imageList)
                })
            }
            resSend(res,200,'获取商品名称以及ID成功',result)
        }
    })
})

// 根据ID获取详情
router.get("/getDetail",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.query.id]
    connection.query(`SELECT * FROM article_table WHERE id=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,'获取详情失败',err)
        }else{
            if (result.length > 0) {
                if(result[0].goodsId != 0){
                    result[0].imageList = JSON.parse(result[0].imageList)
                    connection.query(`SELECT * FROM article_table WHERE id=?`,result[0].goodsId,(error,result2)=>{
                        if(error){
                            resSend(res,500,'获取详情失败',err)
                        }else{
                            result2[0].imageList = JSON.parse(result2[0].imageList)
                            result[0].goodDetails = result2[0]
                        }
                    })
                   setTimeout(()=>{
                        resSend(res,200,'获取详情成功',result[0])
                   },500)
                }else{
                    result[0].imageList = JSON.parse(result[0].imageList)
                    resSend(res,200,'获取详情成功',result[0])
                }
            }else{
                resSend(res,404,'获取详情失败',err)
            }
        }
    })
})
module.exports = router